var urlData = require('../../../../../data/url.js');

Page({ 
  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    courseId:'',
    itemArray: [],
    setDateSelect:[],
    onloadUrl: urlData.urlList.teachingManage.leaveManageteacher.url,
    onloadPostUrl: urlData.urlList.teachingManage.leaveManageteacherPost.url,
    type:[],
    disabled:[],
    LeaveState:['已读','已同意','已拒绝'],
    LeaveType:['病假','事假','公假','其他']
  },
  /*
    加载全部内容
  */
  onLoad: function (e) {
    var that = this;
    wx.getStorage({//获取缓存
      key: "loginConf",
      success: function (res) {
        that.setData({
          userId: res.data.userId
        });
      }
    });
    wx.getStorage({
      key: 'submitCourse',
      success:function(res){
        console.log(res)
        that.setData({
          selected1: false,
          selected: true,
          courseId: res.data.course_id
        })
        wx.request({
          url: that.data.onloadUrl,
          method: "get",
          data: {
            course_id: that.data.courseId,
            state:1
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            if (res.data.code == 0) {
              that.listData(e, res);
            }
          },
          fail: function (res) {
            console.log('刷list数据失败');
          }
        });
      }
    });
  },
  /*
    刷list数据
  */
  listData: function (e, res) {
    var a = Array.apply(null, Array(500)).map(function(item, i) {
        return 'warn';
    });
    var b = Array.apply(null, Array(500)).map(function(item, i) {
        return false;
    });
    this.setData({
      itemArray: res.data.list,
      type : a,
      disabled : b
    });
  },
  /*
    添加课程之后，使按钮变为不可选择的状态
  */
  setButton:function(e){
    var type = this.data.type,
        disabled = this.data.disabled;

    type[e.target.dataset.index] = 'default';
    disabled[e.target.dataset.index] = true;

    this.setData({
      type : type,
      disabled : disabled
    });    
  },
  agreeName: function (e) {//提交按钮
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定同意该请假申请？',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: that.data.onloadPostUrl,
            'content-type': 'application/json',
            method: "get",
            data: {
              response:1,
              leave_id:e.target.dataset.id
            },
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              that.setButton(e);
            },
            fail: function (res) {
              console.log('提交失败');
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  noAgreeName:function (e) {//提交按钮
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定拒绝该请假申请？',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: that.data.onloadPostUrl,
            'content-type': 'application/json',
            method: "get",
            data: {
              response: 0,
              leave_id:e.target.dataset.id
            },
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              that.setButton(e);
            },
            fail: function (res) {
              console.log('提交失败');
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
   /*
    tab切换(左侧)
  */
  selected:function (e) {
    var that = this;
    wx.request({
      url: this.data.onloadUrl,
      method: "get",
      data: {
        course_id:this.data.courseId,
        state:1
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        if (res.data.code == 0) {
          that.listDataLeft(e, res);
        }
      },
      fail: function (res) {
        console.log('123');
      }
    });
    this.setData({
      selected1: false,
      selected: true
    })
  },
    /*
    刷已处理数据
  */
  listDataLeft: function (e, res) {
    this.setData({
      setDateSelect: res.data.list
    });
  },
  /*
    tab切换(右侧)
  */
  selected1: function (e) {
    var that = this;

    /*设置退课按钮颜色用的*/
    var a = Array.apply(null, Array(100)).map(function (item, i) {
      return 'primary';
    });
    var b = Array.apply(null, Array(100)).map(function (item, i) {
      return false;
    });
    /*--------------------*/

    wx.request({
      url: this.data.onloadUrl,
      method: "get",
      data: {
        course_id: this.data.courseId,
        state: 0
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        if (res.data.code == 0) {
          that.listDataLeft(e, res);
        }
      },
      fail: function (res) {
        console.log('456');
      }
    });
    this.setData({
      selected: false,
      selected1: true
    })
  }
})